package com.gitee.jmash.rbac.service;

import java.util.Set;
import java.util.UUID;
import jmash.rbac.protobuf.ChangePhoneEmailReq;
import jmash.rbac.protobuf.ChangeWechatBindReq;
import jmash.rbac.protobuf.ForgotPwdReq;
import jmash.rbac.protobuf.RegisterUserReq;
import org.eclipse.microprofile.jwt.JsonWebToken;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.gitee.jmash.core.orm.tenant.TenantService;
import com.gitee.jmash.rbac.entity.OpensEntity;
import com.gitee.jmash.rbac.entity.OpensEntity.OpensPk;
import com.gitee.jmash.rbac.entity.TokenEntity;
import com.gitee.jmash.rbac.entity.UserEntity;
import com.gitee.jmash.rbac.model.TokenAuthzCodeReq;
import com.gitee.jmash.rbac.model.UserOpenCreateReq;
import com.gitee.jmash.rbac.thirdparty.unionpay.model.UnionUserMobile;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jmash.rbac.protobuf.LoginAppReq;
import jmash.rbac.protobuf.LoginQrcodeReq;
import jmash.rbac.protobuf.LoginReq;
import jmash.rbac.protobuf.LogoutReq;
import jmash.rbac.protobuf.OpensType;
import jmash.rbac.protobuf.OrganUserCreateReq;
import jmash.rbac.protobuf.RefreshTokenReq;
import jmash.rbac.protobuf.ScanCodeCreateUserReq;
import jmash.rbac.protobuf.UpdateUserReq;
import jmash.rbac.protobuf.UserCreateReq;
import jmash.rbac.protobuf.UserEnableKey;
import jmash.rbac.protobuf.UserImportReq;
import jmash.rbac.protobuf.UserStatus;
import jmash.rbac.protobuf.UserUpdateReq;
import jmash.rbac.protobuf.ValidCodeLoginReq;
import xyvcard.wechat.protobuf.OauthUserInfoResp;

/**
 * 用户 rbac_user服务Write接口.
 *
 * @author <a href="mailto:service@crenjoy.com">crenjoy</a>
 */
public interface UserWrite extends TenantService {

  /** 插入实体. */
  public UserEntity insert(String unifiedId, @NotNull @Valid UserCreateReq user);

  /** update 实体. */
  public UserEntity update(@NotNull @Valid UserUpdateReq user);

  /** 根据主键删除. */
  public UserEntity delete(@NotNull UUID entityId);

  /** 根据主键数组删除. */
  public Integer batchDelete(@NotNull @Valid Set<@NotNull UUID> entityIds);

  /** 登录. */
  public TokenEntity login(@NotNull @Valid LoginReq req);

  /** 扫码登录. */
  public TokenEntity loginByQrcode(@NotNull @Valid LoginQrcodeReq req);

  /** 验证码登录. */
  public TokenEntity loginByValidCode(@NotNull @Valid ValidCodeLoginReq req);

  /** 登出. */
  public boolean logout(@NotNull @Valid LogoutReq req);

  /** 锁定用户. */
  public Integer lockUser(@NotNull Set<UUID> userIds, @NotNull UserStatus userStatus);

  /** 审核/取消审核用户. */
  public Integer approvedUser(@NotNull Set<UUID> userIds, boolean approved);

  /** 启用/禁用用户. */
  public boolean enableUser(@NotNull UserEnableKey userEnableKey);

  /** 批量启用/禁用用户. */
  public Integer batchEnableUser(Set<UUID> entityIds, boolean enabled);
  
  /**
   * authorization code -> code.
   */
  public TokenEntity createAuthzCode(@NotNull @Valid TokenAuthzCodeReq token);

  /**
   * 隐藏式 implicit -> access_token.
   */
  public TokenEntity implicitToken(@NotNull @Valid TokenAuthzCodeReq token);
  
  /**
   * code -> access_token,refresh_token.
   */
  public TokenEntity authzCodeToken(@NotBlank String clientId,@NotBlank String code,@NotBlank String subject);

  /** refresh_token -> access_token,refresh_token, id_token for OpenId Connect. */
  public TokenEntity refreshToken(@NotNull @Valid RefreshTokenReq token,boolean idToken);

  /** password --> access_token,refresh_token. */
  public TokenEntity passwordToken(@NotNull JsonWebToken webToken, @NotBlank String clientId,
      String scope);
  
  /** client_credentials --> access_token. */
  public TokenEntity clientToken(@NotNull JsonWebToken webToken, @NotBlank String clientId,
      String scope);

  /** 清理Token. */
  public TokenEntity clearToken(@NotNull String clientId, @NotNull String accessToken);

  /** 用户修改密码. */
  public boolean changePwd(@NotNull String oldPwd, @NotNull String newPwd);

  /** 更换手机号/邮箱. */
  public boolean changePhoneEmail(@NotNull @Valid ChangePhoneEmailReq request);

  /** 更换微信用户绑定. */
  public boolean changeWechatBind(@NotNull @Valid ChangeWechatBindReq request);

  /** 重置密码. */
  public boolean resetPwd(@NotNull UUID userId, @NotNull String pwd, @NotNull String repeatPwd);

  /** 导入用户信息. */
  public String importUser(@NotNull @Valid UserImportReq request) throws Exception;

  /** 解绑三方登录信息. */
  public OpensEntity deleteUserOpens(@NotNull @Valid OpensPk pk);

  public UserEntity updateUser(@NotNull @Valid UpdateUserReq user, @NotNull String userId);

  /** 三方登录. */
  public TokenEntity loginByOpenId(@NotNull String tenant, @NotNull OpensType openType,
      @NotNull String appId, String openId, String unionId,boolean checkPhone,String phoneNumber);

  /** 用户注册、绑定、登录. */
  public TokenEntity createBindLogin(@NotNull @Valid UserOpenCreateReq req);

  /** 更换绑定手机号. */
  public boolean updateBindPhone(@NotNull String mobilePhone);

  /** 创建组织系统用户. */
  public UserEntity createOrganSystemUser();

  /** 登录组织. */
  public TokenEntity loginOrgan(@NotNull String tenant);

  /** 创建组织用户. */
  public UserEntity createOrganUser(@NotNull UserEntity unifiedUser,
      @NotNull OrganUserCreateReq req);

  /** 创建组织用户(导入旧数据管理员). */
  public UserEntity createOrganUser(@NotNull OrganUserCreateReq req);

  /** 扫码创建用户. */
  public TokenEntity createUserByQrcode(@NotNull ScanCodeCreateUserReq req);

  /** 手机号/邮箱注册用户. */
  public TokenEntity registerUser(@NotNull RegisterUserReq req);

  /** 忘记密码/找回密码/重置密码. */
  public boolean forgotPwd(@NotNull ForgotPwdReq req);
  
  /** 微信公众号注册登录用户. */
  public TokenEntity createUserByGzh(@NotNull String tenant,String name,String directoryId,@NotNull String appId,@NotNull OauthUserInfoResp req);
  
  /** 支付宝注册登录用户. */
  public TokenEntity createUserByAlipay(@NotNull String tenant, String name, String directoryId,
      @NotNull String appId, @NotNull AlipaySystemOauthTokenResponse resp,
      @NotNull AlipayUserInfoShareResponse req);
  
  /** 云闪付注册登录用户. */
  public TokenEntity createUserByUnionPay(@NotNull String tenant,String directoryId, @NotNull String appId,@NotNull UnionUserMobile resp);
  
  /** 应用客户端登录（临时）. */
  public TokenEntity loginApp(@NotNull @Valid LoginAppReq req);
}
